<!DOCTYPE html>
<html>

	<head>

		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">
	</head>

	<body style="padding:0px;background-color: white">
		<div>
			<div id="toolbar" class="receipts_toolbar_style"></div>
			<form id="ff" action="#" method="post">
				<table cellpadding="5" class="displaytable" style="text-align: left;width: 80%;margin: 0 auto;">
					<tr>
						<td width="20%">
							<h id="star">* </h>采购员</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 100%" type="text" id="input_PURCHASE_MAN" name="PURCHASE_MAN" data-options="required:'true'" />
						</td>
						<td width="20%">
							<h id="star">* </h>用户组</td>
						<td width="30%">
							<input class="easyui-textbox" style="width: 100%;" type="text" name="CUSTOMER_TYPE" id="input_CUSTOMER_TYPE" data-options="disabled:true"></input>
						</td>

					</tr>
					<tr>
						<td>
							<h id="star">* </h>采购时间</td>
						<td>
							<input class="easyui-datebox" style="width: 100%;" type="text" name="PURCHASE_DATE" id="input_PURCHASE_DATE" data-options="required:'true'"></input>
						</td>
						<td>
							<h id="star">* </h>仓库</td>
						<td>
							<input class="easyui-combobox" style="width: 100%;" type="text" name="STOCKS" id="input_STOREHOUSE" data-options="required:'true'"></input>
						</td>
					</tr>
					<tr>
						<td>
							<h id="star">* </h>合计数量
						</td>
						<td>
							<input class="easyui-textbox" style="width: 100%;" type="text" name="PURCHASE_AMOUNT" id="input_PURCHASE_AMOUNT" data-options="required:'true',disabled:true">
						</td>
					</tr>

					<tr id="nodisplay" style="display: none">
						<td>
							<h id="star">* </h>仓库ID</td>
						<td>
							<input class="easyui-textbox" style="width:100%;" type="text" name="STOREHOUSE_ID" id="input_STOREHOUSE_ID" data-options="prompt:'由系统自动生成，不需填写',disabled:true," />
						</td>
						<td>
							<h id="star">* </h>用户组ID</td>
						<td>
							<input class="easyui-textbox" style="width:100%;" type="text" name="TYPE_ID" id="input_TYPE_ID" data-options="prompt:'由系统自动生成，不需填写',disabled:true," />
						</td>

					</tr>
				</table>
				<table id="tt">

				</table>

				<div style="margin-left: 20%;" id="new_footer">

				</div>
			</form>
		</div>
		<div id="dlg" class="hiddenDialog">
			<iframe id="ifrmEdit" style="width: 100%;height: 100%;" frameborder="0" scrolling="no"></iframe>
		</div>
		<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
		<script type="text/javascript" src="../../../JS/common.js"></script>
		<script type="text/javascript" src="../../../JS/finance.js"></script>
		<script type="text/javascript" src="../../../JS/jquery.edatagrid.js"></script>
		<script type="text/javascript" src="../../../JS/LogisticsShareFunction/CommonShareFunction.js"></script>
		<script type="text/javascript">
			var _detailToolbarData = "";
			_detailToolbarData = AddDetailToolBar(2);
			var currPurchaseData;
			GetDetailTable();
			getRemoteData();
			addFooter($("#new_footer"));
			var data = [{
				"icon": "icon-save",
				"func": "saveOrder()",
				"functionname": "生成采购单"
			}];
			_toolbar = AddReceiptsToolBar(0, data);
			$("#toolbar").append(_toolbar);

			function getRemoteData() //统一获取远程的数据
			{
				var _localData;
				_localData = localStorage.getItem('PurchaseDetailData');
				if(_localData == null) {
					$.messager.alert("警告", "没有采购单的明细数据");
					return;
				}
				currPurchaseData = JSON.parse(_localData);
				var _data = currPurchaseData.rows; //将获取到的数据中字头为rows的数据赋值给_data
				currPurchaseData.PURCHASE_AMOUNT = getAmount(_data);
				$('#ff').form('load', currPurchaseData);
				$('#tt').edatagrid('loadData', _data); //调用easyui的loadData方法，将数据写入id为tt的datagrid中
			}

			function getAmount(_data) {
				var amount = 0;
				var _num = 0;
				for(var i = 0, len = _data.length; i < len; i++) {
					_num = parseFloat(_data[i].EVALUATE_AMOUNT);
					amount += _num;
				}
				return amount;
			}
			
			function GetDetailTable() { //构造datagrid中的数据
				$('#tt').edatagrid({
					height: 310,
					toolbar: _detailToolbarData, //toolbar选择自定义的名为toolbar的工具栏
					collapsible: 'true', //是否折叠：是
					singleSelect: 'true', //是否单选：是
					rownumbers: 'true',
					fitColumns: 'true', //自动调整列：是
					showFooter: 'true',
					autoSave: 'ture',
					columns: [
						[{
							field: 'INTERNALTRADE_ID',
							title: '内部商品ID',
							width: '15%',
							align: 'center',

						}, {
							field: 'INTERNAL_NAME',
							title: '*内部商品名',
							width: '15%',
							align: 'center',

						}, {
							field: 'INTERNAL_TYPE',
							title: '*内部商品类型',
							width: '15%',
							align: 'center',

						}, {
							field: 'AMOUNT',
							title: '*应采购数量',
							width: '15%',
							align: 'center',

						}, {
							field: 'UNIT_NAME',
							title: '*单位',
							width: '15%',
							align: 'center',

						}, {
							field: 'STOCKS',
							title: '*库存量',
							width: '10%',
							align: 'center',

						}, {
							field: 'EVALUATE_AMOUNT',
							title: '*实际采购数量',
							width: '10%',
							align: 'center',
							editor: {
								type: 'numberbox',
								options: {
									precision: 2,
									groupSeparator: ',',
									decimalSeparator: '.',

								}
							}
						}, ]
					],
					onEndEdit: function(index, row, changes) { //在编辑完成或，执行该函数
						currPurchaseData.rows[index].EVALUATE_AMOUNT = row.EVALUATE_AMOUNT;
					 	var _rows = $("#tt").edatagrid('getRows');
					 	var amount = parseFloat(getAmount(_rows));
					 	currPurchaseData.PURCHASE_AMOUNT = amount.toFixed(2);
					 	$("#input_PURCHASE_AMOUNT").textbox('setValue',amount.toFixed(2));
					},
				});
			}

			$('#input_STOREHOUSE').combobox({
				valueField: 'ID',
				textField: 'NAME',
				panelHeight: 'auto',
				mode: 'remote',
				method: 'get',
				url: RemoteLinkAddress + "/logistics/storehouse/listall.do",
				onSelect: function(record) { //当选中数据后调用该function，并得到该combobox对应的valuefield和textfield，赋值给record
					var trade_ids = "";
					var s = currPurchaseData.rows;
					$("#input_STOREHOUSE_ID").textbox("setValue", record.ID)
					for(var i = 0; i < s.length; i++) {
						trade_ids += s[i].INTERNALTRADE_ID + ";";
					}
					getStoreHouseData(record.ID, trade_ids);

				}
			});

			function getStoreHouseData(storehouseid, trade_ids) {
				_url = RemoteLinkAddress + "/logistics/warehouse/getstock.do?STOREHOUSE_ID=" + storehouseid + "&TRADE_IDS=" + trade_ids;
				$.ajax({ //调用ajax方法，从服务器获取到该订单的所有数据
					type: "get",
					url: _url,
					dataType: "json", //服务器返回的数据类型
					success: function(result) {
						if(result != null) {

							var s = currPurchaseData.rows;
							///清空库存量
							for(var i = 0; i < s.length; i++) {
								s[i].STOCKS = 0;
							}
							for(var i = 0; i < s.length; i++) {
								s[i].EVALUATE_AMOUNT = s[i].AMOUNT;
								for(var j = 0; j < result.length; j++) {
									if(s[i].INTERNALTRADE_ID == result[j].INTERNALTRADE_ID) {
										s[i].STOCKS = result[j].STOCK;

										//										s[i].EVALUATE_AMOUNT = s[i].AMOUNT - s[i].STOCKS;
										//										if(s[i].EVALUATE_AMOUNT <= 0) {
										//											s[i].EVALUATE_AMOUNT = 0;
										//										}
									}
								}
							}
							$('#tt').edatagrid('loadData', s);
						 	var amount = getAmount(s);
						 	$("#input_PURCHASE_AMOUNT").textbox('setValue',amount.toFixed(2));
						 	
						}
					}, //请求成功后，接受服务器传来的Json字符串，并做相应的处理
					error: function(xhr, status, error) {
						$.messager.alert("错误", "请求失败!");
					}
				});
			}
			//初始化采购员信息
			initPurchaseMan();

			function saveOrder() {
				
				var isValid = $("#ff").form('validate');
				if(!isValid) {
					$.messager.alert("警告", "有必填项没有填写");
					return;
				}
				lockButton("lock");
				currPurchaseData.PURCHASE_DATE = $("#input_PURCHASE_DATE").textbox("getValue");
				currPurchaseData.STOREHOUSE_ID = $("#input_STOREHOUSE_ID").textbox("getValue");
				currPurchaseData.MEMO = $("#input_MEMO").textbox("getValue");
				currPurchaseData.PURCHASE_MAN = $("#input_PURCHASE_MAN").combobox("getValue");
				currPurchaseData.TRADE_TYPE = getInternalType();
				currPurchaseData.rows = $('#tt').edatagrid("getRows");
				var _senddata = JSON.stringify(currPurchaseData);
				var _url = RemoteLinkAddress + "/logistics/purchaseorder/save.do";
				$.ajax({
					dataType: 'JSON',
					url: _url,
					data: 'a=' + _senddata,
					type: "POST",
					success: function(result) {
						lockButton("unlock");
						$.messager.alert({
							title: '成功',
							msg: '保存成功',
							fn: function() {
								//parent.$('#tabs').tabs('close', "创建采购单");
								localStorage.removeItem('PurchaseDetailData');
							}
						});
						/////////////////////////////
					},
					error: function(result) {
						$.messager.alert("错误", "保存不成功");
					}
				});
			}

			function getInternalType() {
				var rowsData = $("#tt").datagrid('getRows');
				var typeInfo = new Array();
				for(var i = 0; i < rowsData.length; i++) {
					typeInfo.push(rowsData[i].INTERNAL_TYPE);
				}
				return typeInfo.unique(typeInfo).join('/');
			}
		</script>
	</body>

</html>